﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ThatsMeSDK.Core;
using ThatsMeSDK;

namespace TMAppOnWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        TouchScreenPanel.TouchScreenDetector touchReco;
        private string token = "";
        private List<GalleryPicture> pictures;
        int counter = 0;
    
        // Konstruktor
        public MainPage()
        {
            InitializeComponent();
            touchReco = new TouchScreenPanel.TouchScreenDetector();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
        }

        private void btnLoadUser_Click(object sender, RoutedEventArgs e)
        {
            ThatsMeSDK.Core.ServiceHandler.Initialize(DataFormat.Xml);
            ThatsMeSDK.Core.ServiceHandler.GetNewPicsCompleted += new LoadPicturesCompleted(ServiceHandler_GetNewPicsCompleted);
            ThatsMeSDK.Core.ServiceHandler.GetNewPics(20);
        }

        void ServiceHandler_GetNewPicsCompleted(object sender, LoadPicturesCompletedEventArgs e)
        {
            pictures = e.Pictures;
            SetImage();
        }

        private void PageTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            touchReco = new TouchScreenPanel.TouchScreenDetector();
            touchReco.MouseDownPoint = e.GetPosition(this);
        }

        private void PageTitle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            touchReco.MouseUpPoint = e.GetPosition(this);
        }

        private void PageTitle_MouseMove(object sender, MouseEventArgs e)
        {
            touchReco.MouseMovePoint.Add(e.GetPosition(this));
            TouchScreenPanel.TouchScreenDetector.TouchEvent te = touchReco.GetTouchEvent();
            MessageBox.Show(te.ToString());
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            ServiceHandler.Initialize(DataFormat.Xml);
            ServiceHandler.AuthenticateCompleted += new AuthenticateCompleted(ServiceHandler_AuthenticateCompleted);
            ServiceHandler.CountMessagesCompleted +=new CountMessagesCompleted(ServiceHandler_CountMessagesCompleted);
            ServiceHandler.GetMemberByIdCompleted += new GetMemberCompleted(ServiceHandler_GetMemberByIdCompleted);
            ServiceHandler.GetMemberByNickCompleted += new GetMemberCompleted(ServiceHandler_GetMemberByNickCompleted);
            ServiceHandler.GetMessagesCompleted += new LoadMessagesCompleted(ServiceHandler_GetMessagesCompleted);
            ServiceHandler.GetMessageCompleted += new LoadMessageCompleted(ServiceHandler_GetMessageCompleted);
            ServiceHandler.GetNewPicsCompleted +=new LoadPicturesCompleted(ServiceHandler_GetNewPicsCompleted);
            ServiceHandler.GetUserPicCategoriesCompleted += new LoadUserPicCategoriesCompleted(ServiceHandler_GetUserPicCategoriesCompleted);
            ServiceHandler.GetUserPicsCompleted += new LoadPicturesCompleted(ServiceHandler_GetUserPicsCompleted);
            ServiceHandler.SendMessageCompleted += new LoadBooleanCompleted(ServiceHandler_SendMessageCompleted);

            ServiceHandler.Authenticate("tmdotnetapi", ServiceHandler.CalculateMD5("nexpose"));
        }

        void ServiceHandler_GetMessageCompleted(object sender, LoadMessageCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Message.Body);
        }

        void ServiceHandler_SendMessageCompleted(object sender, LoadBooleanCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Value.ToString());
        }

        void ServiceHandler_GetUserPicsCompleted(object sender, LoadPicturesCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Pictures[0].Url.ToString());
        }

        void ServiceHandler_GetUserPicCategoriesCompleted(object sender, LoadUserPicCategoriesCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Categories[0].Title);
        }

        void ServiceHandler_GetMessagesCompleted(object sender, LoadMessagesCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Messages[0].Header.Subject);
        }

        void ServiceHandler_GetMemberByNickCompleted(object sender, GetMemberCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Member.Birthday.ToString());
        }

        void ServiceHandler_GetMemberByIdCompleted(object sender, GetMemberCompletedEventArgs e)
        {
            MessageBox.Show("Result: " + e.Member.Nick);
        }

        void ServiceHandler_CountMessagesCompleted(object sender, CountMessagesCompletedEventArgs e)
        {
            MessageBox.Show("Data: " + e.Count.Inbox.Total.ToString());
        }

        void ServiceHandler_AuthenticateCompleted(object sender, AuthenticateCompletedEventArgs e)
        {
            //MessageBox.Show("Data: " + e.Token);
            this.token = e.Token;

            //ServiceHandler.CountMessages(this.token);
            //ServiceHandler.SendMessage(this.token, "hello world", "broox", "Test Message!");
            //ServiceHandler.GetMemberByNick("broox");
            //ServiceHandler.GetMemberById(178);
            //ServiceHandler.GetMessages(e.Token, 0, 10, MessageType.Both, false);
            //ServiceHandler.GetMessage(6615112, e.Token);
            //ServiceHandler.GetUserPicCategories("tmdotnetapi");
            ServiceHandler.GetUserPics("tmdotnetapi", 21564);
        }

        private void iImageBox_KeyDown(object sender, KeyEventArgs e)
        {
            SetImage();
        }

        private void SetImage()
        {
            ImageSourceConverter sconv = new ImageSourceConverter();
            this.iImageBox.Source = (ImageSource)sconv.ConvertFromString(pictures[counter].SourceBig.ToString());

            if (counter + 1 < pictures.Count - 1)
            {
                counter++;
            }
            else
            {
                counter = 0;
            }

            counter++;
        }

        private void iImageBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SetImage();
        }

        
    }
}